package com.dyz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.DTO.SysUserDTO;
import com.dyz.domain.SysUser;
import com.dyz.model.Result;
import com.dyz.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
@Api(tags = "员工管理")
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;

    @GetMapping
    @ApiOperation("分页查询用户的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "每页显示条数"),
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", paramType = "query")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
   public Result<?> findUsers(@ApiIgnore Page<SysUser> page, String username, String mobile){
       return  sysUserService.findUsers(page,username,mobile);
   }
    @PostMapping("/add")
    @ApiOperation("新增用户")
    @PreAuthorize("hasAuthority('sys_user_create')")
    public Result<?> addUser(@RequestBody SysUserDTO sysUser){
        return sysUserService.addUser(sysUser);
    }
    @PutMapping("/update")
    @ApiOperation("修改用户")
    @PreAuthorize("hasAuthority('sys_user_update')")
    public Result<?> updateUser(@RequestBody SysUserDTO sysUser){
        return sysUserService.updateUser(sysUser);
    }
    @PostMapping("/delete")
    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('sys_user_delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "用户id")
    })
    public Result<?> deleteUser(String userIds){
        return sysUserService.deleteUserByIds(userIds);
    }
    @GetMapping("/{userId}")
    @ApiOperation("根据id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    public Result<?> getUserById(@PathVariable Long userId){
        return sysUserService.getUserById(userId);
    }
}
